package enrique.kurso.util;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.ArcShape;
import android.graphics.drawable.shapes.OvalShape;
import android.util.AttributeSet;
import android.view.View;

public class ProgressView extends View {
    private int complete=0;
    private int fails=0;
    private int _width=0;
    private int _height=0;

    public ProgressView(Context context,AttributeSet attr) {
        super(context,attr);
    }
    
    public ProgressView(Context context) {
        super(context);
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        _height = View.MeasureSpec.getSize(heightMeasureSpec);
        _width = View.MeasureSpec.getSize(widthMeasureSpec);
        _width = _height;
     
        setMeasuredDimension(_height, _height);
    }

    protected void onDraw(Canvas canvas) {
        int x = 0;
        int y = 0;
        
        ShapeDrawable mDrawable = new ShapeDrawable(new OvalShape());
        mDrawable.getPaint().setColor(0xff333333);
        mDrawable.setBounds(x, y, x + _width, y + _height);

        mDrawable.draw(canvas);

        mDrawable = new ShapeDrawable(new ArcShape(-90,complete*360/100));
        mDrawable.getPaint().setColor(0xffffdd00);
        mDrawable.setBounds(x, y, x + _width, y + _height);

        mDrawable.draw(canvas);

        mDrawable = new ShapeDrawable(new ArcShape(-90,(100-fails)*complete*360/10000));
        mDrawable.getPaint().setColor(0xff009955);
        mDrawable.setBounds(x, y, x + _width, y + _height);

        mDrawable.draw(canvas);
    }

    public void setPercent(int complete,int fails) {
        this.complete=complete;
        if(fails>100) fails=100;
        else this.fails=fails;
        this.invalidate();
    }
}
